package com.example.presentshopping.ui.fragment.homefrgme

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Paint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.SupplierListBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.activity.DetailsActivity
import com.example.presentshopping.ui.custom.ChildRecyclerView2
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import wellijohn.org.scrollviewwithstickheader.ChildRecyclerView
import wellijohn.org.scrollviewwithstickheader.ScrollViewWithStickHeader

class Recommenframen(var str: String) : BaseMvpFragment() {

    var recycler_view: ChildRecyclerView2?=null
    private var queryInfoPresenter: PublicPresenter? = null
    var page = 1
    var supplierListBean: SupplierListBean? = null
    var baseRecyclerAdapter: BaseRecyclerAdapter<SupplierListBean.DataBean.ListBean>? = null

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, true, "加载中")
        queryInfoPresenter?.attachView(this)
        recycler_view= view?.findViewById(R.id.swip_layouy)
        setBaseAdapter()
        RgetGoodsList(page)
        recycler_view?.useDefaultLoadMore()
        recycler_view?.loadMoreFinish(false, true)
        recycler_view?.setLoadMoreListener {
            page++
            RgetGoodsList(page)
        }
    }

    //商品列表
    fun setBaseAdapter() {
        // 客户数据View
        recycler_view?.layoutManager = GridLayoutManager(context, 2)
        baseRecyclerAdapter = object : BaseRecyclerAdapter<SupplierListBean.DataBean.ListBean>(
            context,
            null,
            R.layout.item_more_refuel
        ) {
            @SuppressLint("SetTextI18n")
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: SupplierListBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val ivthumb = holder?.getView<ImageView>(R.id.iv_image)
                val tvname = holder?.getView<TextView>(R.id.tv_name)
                val discountinfo = holder?.getView<TextView>(R.id.discount_info)
                val tvdistance = holder?.getView<TextView>(R.id.tv_distance)
                val mLlGoods = holder?.getView<LinearLayout>(R.id.mLlGoods)
                tvname?.text = data.title
                discountinfo?.text = "¥:" + data.marketprice
                tvdistance?.text = "¥:" + data.productprice
                tvdistance?.paint?.flags = Paint.STRIKE_THRU_TEXT_FLAG;
                GlideUtil.load(context, data.thumb, ivthumb)
                mLlGoods?.setOnClickListener {
                    val Init = Intent(context, DetailsActivity().javaClass)
                    Init.putExtra("msid", data.id.toString())
                    Init.putExtra("Activity", "GatherFragment")
                    context?.startActivity(Init)
                }
            }
        }
        recycler_view?.adapter = baseRecyclerAdapter


    }


    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override fun onSuccess(data: String?) {

    }

    override val layoutId: Int
        get() = R.layout.recommen_framen
    //获取商品列表
    fun RgetGoodsList(inv: Int) {
        queryInfoPresenter?.netWorkRequestPost(
            HttpURL.Req_getGoodsList,
            HttpRequest.getGoodsList(str.toInt(), inv, 10),
            object : TaskCallback {
                override fun onSuccess(data: String?) {
                    supplierListBean = GsonUtils.getGson().fromJson(data,
                        SupplierListBean().javaClass)
                    if (page == 1) {
                        baseRecyclerAdapter?.clear()
                    }
                    if (supplierListBean?.data?.list?.size!! > 0) {
                         recycler_view?.loadMoreFinish(false, true)
                        baseRecyclerAdapter?.setData(supplierListBean?.data?.list)
                    } else {
                        recycler_view?.loadMoreFinish(true, false)
                    }

                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {
                    ToastUtil.showMsg(msg)
                }
            })
    }
}